package com.lottery.ruok.lottrey.chat.event.listener;

import com.lottery.ruok.lottrey.chat.event.manager.SecretManager;

import io.rong.imkit.RongIM;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.MessageContent;
import io.rong.message.TextMessage;

/**
 * 发送消息监听
 */
public class OnSendMessageListener implements RongIM.OnSendMessageListener {

    public static final String SECRET = "secret";
    private static OnSendMessageListener onSendMessageListener;

    public static OnSendMessageListener getInstance() {
        if (onSendMessageListener == null) {
            onSendMessageListener = new OnSendMessageListener();
        }
        return onSendMessageListener;
    }

    @Override
    public Message onSend(Message message) {
        MessageContent content = message.getContent();
        if (content instanceof TextMessage) {
            //需要加密
            TextMessage textMessage = (TextMessage) content;
            if (!SECRET.equals(textMessage.getExtra())) {
                //未加密 需要再加密
                textMessage.setExtra(SECRET);
                try {
                    String encodeText = SecretManager.getInstance().encode(textMessage.getContent());
                    textMessage.setContent(encodeText);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return message;
    }

    @Override
    public boolean onSent(Message message, RongIM.SentMessageErrorCode sentMessageErrorCode) {
        return false;
    }
}
